package com.cxrj.zhsq.userManage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cxrj.zhsq.userManage.dao.RoleMapper;
import com.cxrj.zhsq.userManage.entity.Role;
import com.cxrj.zhsq.userManage.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Override
	public Map<String, Object> selectRoleById(String roleId) {
		return roleMapper.selectRoleById(roleId);
	}

	@Override
	public Map<String, Object> selectRolesPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// String str = " 1=1 and user_set = '" +
		// request.getSession().getAttribute("propertyUserId") + "' order by role_id
		// desc";
		// if
		// (request.getSession().getAttribute("propertyRoleId").equals("0084ae3f-3764-48d1-989c-0e9241e178a9"))
		// {
		String str = " 1=1 order by role_id desc";
		// }
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = roleMapper.getRoleCount(str) + "";
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("condition", " where" + str);
		sqlMap.put("length", Integer.parseInt(length));

		List<Map<String, Object>> list = roleMapper.selectRolesPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public void insertRole(Role role, HttpServletRequest request) {
		role.setRoleId(UUID.randomUUID().toString());
		role.setUserSet(request.getSession().getAttribute("propertyUserId").toString());
		roleMapper.insertSelective(role);
	}

	@Override
	public void updateRole(Role role) {
		roleMapper.updateRole(role);
	}

	@Override
	public void deleteRole(String roleId) {
		roleMapper.deleteRole(roleId);
	}

	@Override
	public Map<String, Object> selectRoleName(String roleName) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Role> role = roleMapper.selectByCondition(" role_name='" + roleName + "'");
		if (role != null && role.size() > 0) {
			resultMap.put("result", -1);
		} else {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public void deleteRolesByIds(String[] roleIds) {
		roleMapper.deleteRolesByIds(roleIds);

	}

	@Override
	public List<Map<String, Object>> selectAllRoles(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		// if
		// (!request.getSession().getAttribute("propertyRoleId").equals("0084ae3f-3764-48d1-989c-0e9241e178a9"))
		// {
		// map.put("userId", request.getSession().getAttribute("userId"));
		// }
		return roleMapper.selectAllRoles(map);
	}

	@Override
	public void updateUserSet(Map<String, Object> paramMap) {
		String newUserSet = "";
		String userCode = paramMap.get("userCode").toString();
		String oldUserSet = roleMapper.selectUserSet(paramMap.get("roleId").toString());
		if (oldUserSet == "" || oldUserSet == null) {
			newUserSet = userCode;
		} else if (oldUserSet.indexOf(userCode) < 0) {
			newUserSet = oldUserSet + "+" + userCode;
		} else {
			return;
		}
		paramMap.put("userSet", newUserSet);
		roleMapper.updateUserSet(paramMap);
	}

	@Override
	public List<Map<String, Object>> selectByUserCode(Map<String, Object> map) {
		return roleMapper.selectByUserCode(map);
	}
}
